using Admin2025.Application.Common;
using Admin2025.Application.Dtos;
using Admin2025.Domain.Entities.App;
using Admin2025.Domain.Repositories;
using System;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace Admin2025.Application.Services;

public class AppDepartmentService : IAppDepartmentService
{
    private readonly IRespository<AppDepartment> _deptRepo;

    public AppDepartmentService(IRespository<AppDepartment> deptRepo)
    {
        _deptRepo = deptRepo;
    }

    public async Task<dynamic> CreateDepartmentAsync(CreateDepartmentDto dto)
    {
        var isExists = _deptRepo.Table.Any(x => x.DepartmentName == dto.DepartmentName && x.ParentId == dto.ParentId);
        if (isExists)
        {
            return ApiResult.Fail(3002, "同级下已存在同名部门，请确认重试");
        }
        var dept = new AppDepartment { DepartmentName = dto.DepartmentName, ParentId = dto.ParentId };
        await _deptRepo.CreateAsync(dept);
        return ApiResult.Success(dept, "创建部门成功");
    }

    public async Task<dynamic> DeleteDepartmentAsync(Guid deptId)
    {
        var dept = await _deptRepo.GetByIdAsync(deptId);
        if (dept == null)
        {
            return ApiResult.Fail(4004, "要删除的部门不存在，请确认后重试");
        }
        if (dept.IsDeleted)
        {
            return ApiResult.Fail(4001, "部门已被删除，请确认后重试");
        }
        dept.IsDeleted = true;
        await _deptRepo.UpdateAsync(dept);
        return ApiResult.Success(dept, "删除成功");
    }

    public async Task<dynamic> UpdateDepartmentAsync(Guid deptId, UpdateDepartmentDto dto)
    {
        var dept = await _deptRepo.GetByIdAsync(deptId);
        if (dept == null)
        {
            return ApiResult.Fail(4004, "要修改的部门不存在，请确认后重试");
        }
        if (dept.IsDeleted)
        {
            return ApiResult.Fail(4001, "部门已被删除，请确认后重试");
        }
        dept.DepartmentName = dto.DepartmentName;
        dept.ParentId = dto.ParentId;
        await _deptRepo.UpdateAsync(dept);
        return ApiResult.Success(dept, "更新成功");
    }

    public async Task<dynamic> EnableDepartment(Guid deptId)
    {
        var dept = await _deptRepo.GetByIdAsync(deptId);
        if (dept == null)
        {
            return ApiResult.Fail(4004, "部门不存在，请确认后重试");
        }
        if (dept.IsDeleted)
        {
            return ApiResult.Fail(4001, "部门已被删除，请确认后重试");
        }
        if (dept.IsActived)
        {
            return ApiResult.Fail(4001, "部门已启用，无需重复启用");
        }
        dept.IsActived = true;
        await _deptRepo.UpdateAsync(dept);
        return ApiResult.Success(dept, "启用成功");
    }

    public async Task<dynamic> DisableDepartment(Guid deptId)
    {
        var dept = await _deptRepo.GetByIdAsync(deptId);
        if (dept == null)
        {
            return ApiResult.Fail(4004, "部门不存在，请确认后重试");
        }
        if (dept.IsDeleted)
        {
            return ApiResult.Fail(4001, "部门已被删除，请确认后重试");
        }
        if (!dept.IsActived)
        {
            return ApiResult.Fail(4001, "部门已禁用，无需重复禁用");
        }
        dept.IsActived = false;
        await _deptRepo.UpdateAsync(dept);
        return ApiResult.Success(dept, "禁用成功");
    }
}
